﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;

namespace MyTest
{
    /// <summary>
    /// 教员:薛海
    /// 微信:xuehai03
    /// QQ:289477003
    /// 小型资源管理器
    /// 2019-2-20
    /// </summary>
    public partial class frmSource : Form
    {
        public frmSource()
        {
            InitializeComponent();
        }

        //窗体加载事件
        private void frmSource_Load(object sender, EventArgs e)
        {
            TreeNode node = new TreeNode();
            node.Text = @"D:\";
            node.Tag = @"D:\";
            this.treeView1.Nodes.Add(node);

        }

        //绑定的方法
        public void BindInfo(TreeNode node)
        {
            DirectoryInfo dir = new DirectoryInfo(node.Tag.ToString());

            DirectoryInfo[] dirs = dir.GetDirectories();

            foreach (DirectoryInfo item in dirs)
            {
                TreeNode tn = new TreeNode();
                tn.Text = item.Name;
                tn.Tag = item.FullName;
                node.Nodes.Add(tn);
            }

            FileInfo[] info = dir.GetFiles();

            List<myFile> file = new List<myFile>();

            foreach (FileInfo item in info)
            {
                myFile myfile = new myFile();
                myfile.FileName = item.Name;
                myfile.FileLength = item.Length;
                myfile.FileType = item.Extension;
                myfile.FilePath = item.FullName;

                file.Add(myfile);
            }

            ListViewItem items = null;
            listView1.Items.Clear();

            foreach (myFile item in file)
            {
                items = new ListViewItem();
                items.Text = item.FileName;
                items.SubItems.Add(item.FileLength.ToString());
                items.SubItems.Add(item.FileType);
                items.SubItems.Add(item.FilePath);

                listView1.Items.Add(items);
            }


        }

        //点击菜单的事件
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode node = this.treeView1.SelectedNode;

            BindInfo(node);
        }


        //数据导出事件
        private void btnOut_Click(object sender, EventArgs e)
        {
            ExportToExecl();
        }

        public void ExportToExecl()
        {
            //打开计算机窗口
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.DefaultExt = "xls";
            sfd.Filter = "Excel文件(*.xls)|*.xls";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                DoExport(this.listView1, sfd.FileName);
            }
        }


        //导出数据的方法
        private void DoExport(ListView listView, string strFileName)
        {
            //获得listview的项目数
            int rowNum = listView.Items.Count;
            //获得listview项的子项
            int columnNum = listView.Items[0].SubItems.Count;
            int rowIndex = 1;
            int columnIndex = 0;
            if (rowNum == 0 || string.IsNullOrEmpty(strFileName))
            {
                return;
            }
            if (rowNum > 0)
            {

                //excel文件对象
                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

                try
                {
                    if (xlApp == null)
                    {
                        MessageBox.Show("无法创建excel对象，可能您的系统没有安装excel");
                        return;
                    }
                    xlApp.DefaultFilePath = "";
                    xlApp.DisplayAlerts = true;
                    xlApp.SheetsInNewWorkbook = 1;
                    Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
                    //将ListView的列名导入Excel表第一行
                    foreach (ColumnHeader dc in listView.Columns)
                    {
                        columnIndex++;
                        xlApp.Cells[rowIndex, columnIndex] = dc.Text;
                    }
                    //将ListView中的数据导入Excel中
                    for (int i = 0; i < rowNum; i++)
                    {
                        rowIndex++;
                        columnIndex = 0;
                        for (int j = 0; j < columnNum; j++)
                        {
                            columnIndex++;
                            //注意这个在导出的时候加了“\t” 的目的就是避免导出的数据显示为科学计数法。可以放在每行的首尾。
                            xlApp.Cells[rowIndex, columnIndex] = Convert.ToString(listView.Items[i].SubItems[j].Text) + "\t";
                        }
                    }
                    //例外需要说明的是用strFileName,Excel.XlFileFormat.xlExcel9795保存方式时 当你的Excel版本不是95、97 而是2003、2007 时导出的时候会报一个错误：异常来自 HRESULT:0x800A03EC。 解决办法就是换成strFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal。
                    xlBook.SaveAs(strFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
                        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                        Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
                        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    
                    xlBook = null;
                    MessageBox.Show("导出成功!!", "海哥提示");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("导出失败" + ex.Message, "海哥提示");
                }
                finally
                {
                    xlApp.Quit();
                    xlApp = null;
                }
               
            }
        }
    }
}
